package com.example.p001_interview03;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.LockSupport;

/**
 * 两个线程，一个打印ABC...Z，另外一个打印123...26，实现交替顺序打印A1B2C3...Z26
 */
public class CrossPrinter01 {

    static List<String> letters = new ArrayList<>();
    static List<String> numbers = new ArrayList<>();

    static {
        for (int i = 'A'; i <= 'Z'; i++) {
            letters.add(String.valueOf((char) i));
        }
        for (int i = 1; i <= 26; i++) {
            numbers.add(String.valueOf(i));
        }
    }

    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {

        letters.forEach(System.out::print);
        System.out.println();
        numbers.forEach(System.out::print);
        System.out.println();

        t1 = new Thread(() -> {

            letters.forEach(s -> {
                System.out.print(s);
                LockSupport.unpark(t2);
                LockSupport.park();
            });

        });
        t2 = new Thread(() -> {
            numbers.forEach(s -> {
                LockSupport.park();
                System.out.print(s);
                LockSupport.unpark(t1);
            });
        });

        t1.start();
        t2.start();
    }

}
